<?php

global $CFG;
require_once('base.php');
require_once( $CFG->dirroot . 'mod/folio/model/tags.php');
require_once( $CFG->dirroot . 'mod/folio/model/SimpleSuggest.php');

/**
 * Used to show iframe content in a popup.
 */
class folio_TagListViewer extends folio_BaseView{

    /**
     * Show a list of pages matching the tag with pop-ups
     *
     * @param folio_tag $tag
     * @param int $start What record to beging returning.
     * @param int $limit How many pages to return.
     */
    function __construct( $tag, $start, $limit ) {
        global $CFG;
        $url = url;
        $pre = 'folio_TagListViewer_Item';

        $pages = folio_SimpleSuggest::FromTag($tag, $start, $limit );

        $items = '';

        if( $pages != false AND count($pages) > 0 ) {
            foreach( $pages as $page ) {
		
                $user = folio_user::SelectWhereIdent( $page->user_ident ) ;

                $linktitle = folio_page::title_encode( $page->title);
                $safetitle = str_replace('"',"\\'\\'",
                             str_replace("'","\\'", $page->title));
                $items .= <<< END
<li>
    <a id='{$pre}{$page->page_ident}link' href="{$url}{$user->username}/page/{$linktitle}"
    onmouseover="javascript:folio_ShowPanel('{$page->page_ident}','{$safetitle}',
        '{$url}mod/folio/ajax/page_select_popup.php?page_ident={$page->page_ident}');"
    onmouseout="javascript:folio_PanelHide('YAHOO.example.container.panel1');"
    >

        <img alt="User Icon" height='30px' src="{$CFG->wwwroot}_icon/user/{$user->icon}"/>
        {$user->name}: {$page->title}
    </a>
    <div id='folio_ResizePanel{$page->page_ident}popup'></div>
END;
            }
        }

        $oHelp = __gettext('Hover your cursor to preview, or click to ' .
                           'open them in a new window.');
        $this->html .= <<< END
<div class="folio_page_help">Pages from Tag '{$tag}'<br/>{$oHelp}</div>
<ul>
    {$items}
</ul>
END;

        $this->metatags = array(
            'mod/folio/lib/yui_2.4.1/build/yahoo/yahoo-min.js',
            'mod/folio/lib/yui_2.4.1/build/event/event-min.js',
            'mod/folio/lib/yui_2.4.1/build/dom/dom-min.js',
            'mod/folio/lib/yui_2.4.1/build/dragdrop/dragdrop-min.js',
            'mod/folio/lib/yui_2.4.1/build/animation/animation-min.js',
            'mod/folio/lib/yui_2.4.1/build/container/container.js',
            'mod/folio/js/ResizePanel.js',
            'mod/folio/lib/yui_2.4.1/build/container/assets/container.css',
            'mod/folio/css/ResizePanel.css'
        );
    }
}

class folio_TagLinkViewer extends folio_BaseView{

    private $page;

    /**
     * Show a list of pages with pop-ups
     * @param folio_page $page
     */
    function __construct( folio_page $page ) {
        $this->page = $page;
        global $CFG;
        $url = url;
        $pre = 'folio_TagLinkViewer_Item';

        $pages = folio_SimpleSuggest::RelatedPages($page);

        $items = '';

        if( $pages != false AND count($pages) > 0 ) {
            foreach( $pages as $page ) {
				// Add log entry to show that this page has been viewed.
				folio_history_pagelog($page, 'popup_listed' );

                $user = folio_user::SelectWhereIdent( $page->user_ident ) ;

                $linktitle = folio_page::title_encode( $page->title);
                $safetitle = str_replace('"',"\\'\\'",
                             str_replace("'","\\'", $page->title));
                $items .= <<< END
<li>
    <a id='{$pre}{$page->page_ident}link' href="{$url}{$user->username}/page/{$linktitle}"
    onmouseover="javascript:folio_ShowPanel('{$page->page_ident}','{$safetitle}',
        '{$url}mod/folio/ajax/page_select_popup.php?page_ident={$page->page_ident}');"
    onmouseout="javascript:folio_PanelHide('YAHOO.example.container.panel1');"
    >
        <img alt="User Icon" height='30px' src="{$CFG->wwwroot}_icon/user/{$user->icon}"/>
        {$user->name}: {$page->title}
    </a>
    <div id='folio_ResizePanel{$page->page_ident}popup'></div>

END;
            }
        }

        $oHelp = __gettext('Hover your cursor to preview, or click to ' .
                           'open them in a new window.');
        $this->html .= <<< END

<h1>Similar Pages</h1>
<div class="folio_page_help">{$oHelp}</div>
<ul>
    {$items}
</ul>
END;

        $this->metatags = array(
            'mod/folio/lib/yui_2.4.1/build/yahoo/yahoo-min.js',
            'mod/folio/lib/yui_2.4.1/build/event/event-min.js',
            'mod/folio/lib/yui_2.4.1/build/dom/dom-min.js',
            'mod/folio/lib/yui_2.4.1/build/dragdrop/dragdrop-min.js',
            'mod/folio/lib/yui_2.4.1/build/animation/animation-min.js',
            'mod/folio/lib/yui_2.4.1/build/container/container.js',
            'mod/folio/js/ResizePanel.js',
            'mod/folio/lib/yui_2.4.1/build/container/assets/container.css',
            'mod/folio/css/ResizePanel.css'
        );
    }
}

class folio_TagViewer extends folio_BaseView{

    private $page;

    function __construct( folio_page $page ) {
        $this->page = $page;
        global $CFG;
        $url = url;
        $t = array();

        // Load tags for page
        $tags = folio_tags::SelectWherePageIdent( $this->page->page_ident);

        if ( is_array( $tags ) AND count( $tags ) > 0 ) {
            // Turn tags into links.
            foreach ( $tags as $tag ) {
                $t[] = "<a href='{$url}mod/folio/html/view_related.php?tag={$tag->tag}'>{$tag->tag}</a>";
            }

            // Build lables
            $oLabel = __gettext('Tags');
            $tags = implode( ', ', $t );

            // Build html template.
            $tags = <<< END
            <div class="folio_tags"><h3>{$oLabel}</h3> {$tags}</div>
END;

        } else {
            // No tags
            $tags = '';
        }

        $this->html = $tags;
    }

}

class folio_TagAutocomplete extends folio_BaseView {

    private $page;

    function __construct( folio_page $page ) {
        $this->page = $page;
        $this->html = $this->_html();
        $this->js = $this->_js();
        $this->metatags = $this->_metatags();

        $this->Load( new folio_Tooltip('autocomplete_examples',
            _gettext('Click here to add tags to your page.  Tags will help other ' .
                     'people find your portfolio.  They are also used for portfolios ' .
                     'so that staff can find pages submitted in fulfillment of ' .
                     'a particular qualification.')));
    }

    /**
     * YUI-based control for autocomplete tags.
     *
     * @param folio_page $page
     * @return string
     */
    private function _html( ) {
        $tags = folio_tags::toString(
                    folio_tags::SelectWherePageIdent($this->page->page_ident)
                    );

        $oTitle = __gettext('Tags');

        $html = <<< END
<tr><td class='folio_edit'>
    <span class="folio_page_sectiontitle">{$oTitle}:</span>
</td><td class='folio_edit'>
    <div id='folio_TagAutocomplete' class="autocomplete">
        <div id="ysearchinput" class="folio_page_editable">{$tags}</div>
        <div id="ysearchcontainer" ></div>
    </div>
</td></tr>
END;
//        <input id="ysearchinput" type="text">

        return $html;
    }

    /**
     * Return the js for the control
     *
     * @param folio_page $page
     * @return string
     */
    private function _js( ) {
        $url = url;
        $js = <<< JS
    // Set as editable.
    folio_ajax_makeHtmlEditable('{$this->page->page_ident}','tags', 'tags', 'ysearchinput','folio_tags_autocomplete_init();');
JS;

        return $js;
    }


    /**
     * Return the js libraries
     *
     * @return array of filenames (no url) local to the folio plug-in.
     */
    private function _metatags() {
        return array(     'mod/folio/lib/yui_2.4.1/build/fonts/fonts-min.css',
                        'mod/folio/lib/yui_2.4.1/build/autocomplete/assets/skins/sam/autocomplete.css',
                        'mod/folio/lib/yui_2.4.1/build/yahoo-dom-event/yahoo-dom-event.js',
                        'mod/folio/lib/yui_2.4.1/build/animation/animation-min.js',
                        'mod/folio/lib/yui_2.4.1/build/connection/connection-min.js',
                        'mod/folio/lib/yui_2.4.1/build/autocomplete/autocomplete-min.js',
                        'mod/folio/js/tags.js',
                        'mod/folio/css/view_tags.css'
                    );
    }

}

?>
